#include "Key.h"

// 初始化
void Key_init() {
	//准双向口	P05
	P0_MODE_IO_PU(GPIO_Pin_5);
}

// 抬起1，按下0
#define  DOWN	0
#define  UP		1

static u8 last_state = UP;

//扫描按键
void Key_Scan(){
    if(KEY == DOWN && last_state == UP){
        last_state = DOWN;
        #if USE_KEYDOWN
        Key_Down();
        #endif
    }else if(KEY == UP && last_state == DOWN){
        last_state = UP;
        #if USE_KEYUP
        Key_Up();
        #endif
    }
}

//扫描按键，函数指针回调函数版本
//不要和Key_Scan()一起使用
void Key_Scan_Callback(void (*down)(), void (*up)()){
    if(KEY == DOWN && last_state == UP){
        last_state = DOWN;
        if(down != NULL)
            down();
    }else if(KEY == UP && last_state == DOWN){
        last_state = UP;
        if(up != NULL)
            up();
    }
}
